package com.mzwf.cloud.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;

import javax.servlet.http.HttpServletRequest;

public class PreRequestLogFilter extends ZuulFilter {

    private static final Logger LOGGER= LoggerFactory.getLogger(PreRequestLogFilter.class);
    @Override
    public String filterType() {
        //pre类型的过滤器
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() { //返回一个int值来指定过滤器的执行顺序，不同的过滤器允许返回相同的数字
        //org.springframework.cloud.netflix.zuul.filters.pre.PreDecorationFilter 之前执行
        return FilterConstants.PRE_DECORATION_FILTER_ORDER-1;
    }

    @Override
    public boolean shouldFilter() {
        //返回一个boolean值来判断该过滤器是否要执行，true表示要执行，false表示要执行
        return true;
    }

    @Override
    public Object run() {
        //过滤器的具体逻辑
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        PreRequestLogFilter.LOGGER.info(String.format("send %s request to %s", request.getMethod(), request.getRequestURL().toString()));
        return null;
    }
}
